﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevMed.Library;
using DevMed.Models;
using System.Web.Security;
using System.Web.Routing;
using System.Data.SqlClient;

namespace DevMed.Controllers
{
    [Authorize(Roles="Administrador")]
    public class AdministracaoController : Controller
    {
        //
        // GET: /Administracao/

        public ActionResult Index()
        {
            Apoio apoio = new Apoio();
            
            if (!apoio.IsUserClinica(User.Identity.Name))
            {
                return RedirectToAction("CadastrarClinica", "Administracao");
            }
            else
            {
                return View();
            }
        }

        //
        // GET: /Administracao/CadastrarClinica

        public ActionResult CadastrarClinica()
        {
            return View();
        }

        //
        // POST: /Administracao/CadastrarClinica

        [HttpPost]
        public ActionResult CadastrarClinica(CadastrarClinicaModels clinica)
        {
            try
            {
                Conexao conexao = new Conexao(CadastrarClinicaModels.NOMETABELA);
                conexao.AdicionaParametro("Nome", clinica.Nome);
                conexao.AdicionaParametro("Razao_Social", clinica.Razao_Social);
                conexao.AdicionaParametro("Cnpj", clinica.Cnpj);
                conexao.AdicionaParametro("Endereco", clinica.Endereco);
                conexao.AdicionaParametro("Numero", clinica.Numero);
                conexao.AdicionaParametro("Bairro", clinica.Bairro);
                conexao.AdicionaParametro("Cep", clinica.Cep);
                conexao.AdicionaParametro("Cidade", clinica.Cidade);
                conexao.AdicionaParametro("Estado", clinica.Estado);
                conexao.AdicionaParametro("Categoria", clinica.Categoria);
                conexao.AdicionaParametro("UserName", User.Identity.Name);
                conexao.Executa(Comandos.Insere);
                
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Administracao/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Administracao/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Administracao/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Administracao/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Administracao/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Administracao/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Administracao/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
